package com.yj.shortLink;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

@Slf4j
@RestController
//@Controller
@RequestMapping("/shortLink")
public class ShortLinkController {

    @Autowired
    private ShortLinkService shortLinkService;


    @GetMapping("/create")
    @ResponseBody
    public String createShortLink(String longUrl) {
        String shortUrl = shortLinkService.createShortLink(longUrl);
        return shortUrl;
    }

    @GetMapping("/{shortUrl}")
    public RedirectView redirectToLongUrl(@PathVariable String shortUrl) {
    //public String redirectToLongUrl(@PathVariable String shortUrl) {
        String longUrl = shortLinkService.parseShortLink(shortUrl);
        //return "redirect:" + longUrl;
        return new RedirectView(longUrl);
    }

    @GetMapping("/check")
    public boolean checkShortLink(String shortUrl) {
        return shortLinkService.validateShortLink(shortUrl);
    }
}
